package com.hzy.fivepalace.db.service;

import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.hzy.fivepalace.common.type.OrderStatusEnums;
import com.hzy.fivepalace.common.util.OrderUtils;
import com.hzy.fivepalace.common.util.ResponseUtil;
import com.hzy.fivepalace.db.dao.DtsOrderMapper;
import com.hzy.fivepalace.db.dao.ex.OrderMapper;
import com.hzy.fivepalace.db.domain.DtsOrder;
import com.hzy.fivepalace.db.domain.DtsOrderWithBLOBs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 退款服务
 */
@Service
public class RefundService {

    private static Logger log = LoggerFactory.getLogger(PayService.class);

    @Autowired
    private WxPayService wxPayService;

    @Resource
    private DtsOrderMapper dtsOrderMapper;

    @Value("${hzy.wx.refund-notify-url}")
    private String wxRefundCallbackUrl;


    /**
     * 发起微信退款
     * @param order
     * @param refundDesc 退款描述
     * @param isAlone 是否是单独退款
     * @return
     */
    public Object initiateWechatRefund(DtsOrder order,String refundDesc,boolean isAlone) {
        try {
            //金额转为分
            Integer money = order.getActualPrice().multiply(new BigDecimal(100)).intValue();
            WxPayRefundRequest request = new WxPayRefundRequest();
            //商户订单号
            request.setOutTradeNo(order.getOutTradeNo());
            //商户退款单号
            request.setOutRefundNo(OrderUtils.generateRefundOrderNo());
            //订单金额
            request.setTotalFee(money);
            //退款金额
            request.setRefundFee(money);
            //操作员为系统
            request.setOpUserId("system");
            //退款原因
            request.setRefundDesc(refundDesc);
            request.setNotifyUrl(wxRefundCallbackUrl);
            wxPayService.refund(request);
            //订单状态改为退款处理中
            DtsOrderWithBLOBs orderUpd = new DtsOrderWithBLOBs();
            orderUpd.setId(order.getId());
            orderUpd.setUpdateTime(LocalDateTime.now());
            orderUpd.setOrderStatus((short) OrderStatusEnums.TYPE7.getType());
            orderUpd.setRefundDesc(refundDesc);
            if(isAlone){
                orderUpd.setAfterSaleDescribe(refundDesc);
            }else{
                //商家售后处理时间
                orderUpd.setAfterSaleDisposeTime(LocalDateTime.now());
            }
            dtsOrderMapper.updateByPrimaryKeySelective(orderUpd);
            return ResponseUtil.ok("操作成功,退款处理中",null);
        } catch (WxPayException e) {
            log.error("----------订单退款失败,orderId:{},msg:{}----------", order.getId(), e.getMessage());
            return ResponseUtil.fail("发起退款失败,"+e.getMessage());
        }
    }


}

